package three_Thread;

public class SellTicket implements Runnable{

    private int tickets = 1000;
    private Object obj = new Object();
    private int x = 0;

    @Override
    public void run() {
        while (true) {

            if (x % 2 == 0) {
                sellTicket();
            } else {
                sellTicket();

            }
            x++;
/*
            synchronized (obj) {
                if (tickets > 0) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "正在卖第" + tickets + "张票");
                    tickets--;
                }
            }
*/
        }
    }

    // 锁是this
    private synchronized void sellTicket() {
        if (tickets > 0) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "正在卖第" + tickets-- + "张票");
        }
    }
}
